package com.axxc.jpa.specification;

import java.io.Serializable;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.axxc.jpa.entity.HumanEntity;
import com.axxc.jpa.out.model.Human;

/**
 * @ClassName: HumanSpec
 * @Description: 查询筛选
 * @author 熊小川
 * @date 2018年5月13日 下午5:04:35
 */
public class HumanSpec implements Serializable, Specification<HumanEntity> {

	private static final long serialVersionUID = 1L;

	private Human human;

	public HumanSpec(Human human) {
		super();
		this.human = human;
	}

	@Override
	public Predicate toPredicate(Root<HumanEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		// TODO 筛选条件
		Predicate predicate = null;
		if (human != null) {
			predicate = cb.conjunction();

			if (StringUtils.isNotBlank(human.getName())) {
				predicate=cb.and(predicate, cb.like(root.get("name"), "%" + human.getName() + "%"));
			}

		}
		return predicate;
	}

}
